<?php
class C_EyeRefraction extends Controller {

	public function C_EyeRefraction($template_mod = "general") {
		parent::Controller();
		$this->template_mod = $template_mod;
		$this->view->path 	= 'eye_refraction';
	}	

	public function actionLoadEyeRefraction(){
		$recordId	=	(int)$_GET['recordId'];
		$refraction	=	ORDataObject::factory('EncounterRefraction')->getObjectFromRecordId($recordId);
		$commonInfo = 	ORDataObject::factory('EncounterCommonInfo')->getObjectFromRecordId($recordId);

		if($refraction->getId() > 0){
			$doctorName	=	$refraction->getDoctorName();
			$doctorId	=	$refraction->getDoctorId();
			$doctorUpdate = $refraction->getUpdatedDoctorName();
			$created = L10nHelper::renderDateTime($refraction->get('encounter_date'), 'medium', 'datetime');
			$updated = L10nHelper::renderDateTime($refraction->get('last_updated'), 'medium', 'datetime');
		} else {
			$person		= ORDataObject::factory("Person");
			$person->setup($this->_me->get_person_id());
			$doctorName	=	$person->getName();
			$doctorId	=	$person->getId();
			$doctorUpdate = $doctorName;
			$created = L10nHelper::renderDateTime(date('Y-m-d H:m:s'), 'medium', 'datetime');
			$updated = L10nHelper::renderDateTime(date('Y-m-d H:m:s'), 'medium', 'datetime');
		}
		$config		=	Celini::configInstance();
		$checked	=	strtolower($config->get('default_print_signature'));
		$patientId = Celini::newORDO('MedicalRecord', $recordId)->get('patient_id');
		$this->assign_by_ref('patient', Celini::newORDO('Patient', $patientId));
		$this->assign_by_ref('refraction',$refraction);
		$this->assign_by_ref('commonInfo',$commonInfo);
		$this->assign('recordId',$recordId);
		$this->assign('doctorName',$doctorName);
		$this->assign('doctorId',$doctorId);
		$this->assign('doctorUpdate',$doctorUpdate);
		$this->assign('createdDate', $created);
		$this->assign('updatedDate', $updated);
		$this->assign('checked', $checked);
		return $this->view->render('edit_eye_refraction.html');
	}

	public function actionEditEyeRefraction($refractionId = 0){
		if(isset($_POST['refractionId'])){
			$refractionId	=	(int)$_POST['refractionId'];
		}
		$refraction	=	ORDataObject::factory('EncounterRefraction');
		$commonInfo = 	ORDataObject::factory('EncounterCommonInfo');
		$encounterDate = '';
		if($refractionId != 0){
			$refraction->setId($refractionId);
			$encounterDate = 'already';
		}
		$commonInfoId = $commonInfo->getIdFromRecordId($_POST['recordId']);
		if ($commonInfoId > 0) {
			$commonInfo->setup($commonInfoId);
		}
		if(isset($_POST['doctorId'])&& $_POST['doctorId']!=''){
			$doctor_id	=	$_POST['doctorId'];
		} else {
			$doctor_id	=	$this->_me->get_person_id();
		}
		$doctor_update_id = $this->_me->get_person_id();
		$refraction->setData($_POST['recordId'], $_POST['leftRefractionType'], $_POST['rightRefractionType'],
			$_POST['interocularDistance'], $_POST['leftCorrectGlass'], $_POST['rightCorrectGlass'],
			$_POST['leftVisualAcuity'], $_POST['rightVisualAcuity'], $_POST['leftSkiascopy'],
			$_POST['rightSkiascopy'], $_POST['leftCurrentDiopter'], $_POST['rightCurrentDiopter'],
			$_POST['KCTK'], $_POST['leftSuggestion'], $_POST['rightSuggestion'], $doctor_id, $doctor_update_id, $_POST['refNote']);

		$commonInfo->setData($_POST['recordId'], $_POST['leftJavalK1'], $_POST['leftJavalK2'],
			$_POST['rightJavalK1'], $_POST['rightJavalK2'], $_POST['leftCVA'], $_POST['leftUCVA'],
			$_POST['rightCVA'], $_POST['rightUCVA'], $_POST['bothEyesight']);


		$patientId = Celini::newORDO('MedicalRecord', $_POST['recordId'])->get('patient_id');
		$p = Celini::newORDO('Patient', $patientId);
		if (isset($_POST['potentialLasik']) && $_POST['potentialLasik'] == 'on') {
			$p->set('potential_for', 'lasik');
		} else {
			$p->set('potential_for', '');
		}
		$p->persist();

		if($refraction->persist($encounterDate)){
			$commonInfo->persist();
			$response['saved'] = 1;
			$response['refId'] = $refraction->get('id');
		}
		else{
			$response['saved'] = 0;
		}
		echo json_encode($response);
	}

}
?>